﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using Hx.Client.Models;
using Hx.Components.Models;
using Hx.Infrastructure.Authorization;
using Hx.Infrastructure.Helpers;
using Application = System.Windows.Forms.Application;
using Cursor = System.Windows.Input.Cursor;
using Cursors = System.Windows.Input.Cursors;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;

namespace Hx.Client
{
    public partial class MainWindow
    {
        private const int WM_SYSCOMMAND = 0x112;
        private HwndSource _HwndSource;
        private NotifyIcon _notifyIcon;

        private readonly Dictionary<ResizeDirection, Cursor> _cursors = new Dictionary<ResizeDirection, Cursor>
        {
            {ResizeDirection.Top, Cursors.SizeNS},
            {ResizeDirection.Bottom, Cursors.SizeNS},
            {ResizeDirection.Left, Cursors.SizeWE},
            {ResizeDirection.Right, Cursors.SizeWE},
            {ResizeDirection.TopLeft, Cursors.SizeNWSE},
            {ResizeDirection.BottomRight, Cursors.SizeNWSE},
            {ResizeDirection.TopRight, Cursors.SizeNESW},
            {ResizeDirection.BottomLeft, Cursors.SizeNESW}
        };

        public MainWindow(AccessTokenResult token)
        {
            WebApiHelper.SetAuthentication("Bearer", token.AccessToken);

            InitializeComponent();

            Notify();

            InitHeader();

            SourceInitialized += (object sender, EventArgs e) =>
                {
                    _HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
                };

            MouseMove += Window_MouseMove;

        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private void Notify()
        {
            _notifyIcon = new NotifyIcon
            {
                Text = @"Simple Is More!",
                Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath),
                Visible = true
            };

            var menu1 = new MenuItem("新随记");
            menu1.Click += menu1_Click;

            var menu2 = new MenuItem("退出");
            menu2.Click += menu2_Click;

            _notifyIcon.ContextMenu = new ContextMenu(new[] { menu1, menu2 });
            _notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
        }

        void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Show();
        }

        private void InitHeader()
        {
            BeforeCallback();
            var author = WebApiHelper.PostResponse<AuthorModel>(UrlDefinition.GetAccount, null);
            AfterCallback();
            Header.SetAuthor(author);
        }

        private void BeforeCallback()
        {
            Loading.Visibility = Visibility.Visible;
        }

        private void AfterCallback()
        {
            Loading.Visibility = Visibility.Collapsed;
        }

        private void menu2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menu1_Click(object sender, EventArgs e)
        {
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed)
            {
                var element = e.OriginalSource as FrameworkElement;
                if (element != null && !element.Name.Contains("Resize"))
                    Cursor = Cursors.Arrow;
            }
        }

        private void ResizePressed(object sender, MouseEventArgs e)
        {
            var element = sender as FrameworkElement;
            var direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", ""));

            Cursor = _cursors[direction];
            if (e.LeftButton == MouseButtonState.Pressed)
                ResizeWindow(direction);
        }

        private void ResizeWindow(ResizeDirection direction)
        {
            SendMessage(_HwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
        }

        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }
    }
}